/*
Section: Placebo estimations
Content: Figure G.3
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Journal: Journal of Labor Economics
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/

********************************************************************************
**# LOAD ANALYSIS DATASET
********************************************************************************
	
use "$final/finaldata_ieb_main_light.dta", replace
	
********************************************************************************
**# RUN PLACEBO EVENTS ROUTINE + MERGE TO THE ANALYSIS DATASET
********************************************************************************

cap drop startmy 

g startmy = ym( year(match_date),month(match_date))
format startmy %tm
*clonevar ieb_nat_id = nat_first

drop *terror* 
forvalues rep = 1(1)300 {
	
	
	preserve
	do "$prog/event_prep/2_IEB_create_PLACEBO_monthly_events_final.do"
	restore
	

	preserve 

cap drop merge_placebo*
merge m:1 startmy ieb_nat_id using "$data\prepost_setup_placebo.dta", gen(merge_placebo`rep') keep(1 3)
	

**# GENERATE NECESSARY TREATMENT VARIABLES
cap drop max_treatment_terror_m3y_p90 min_treatment_terror_m3y_p90 s_treatment_terror_m3y_p90

bysort ieb_nat_id : egen max_treatment_terror_m3y_p90=max(treatment_terror_m3y_p90)
bysort ieb_nat_id : egen min_treatment_terror_m3y_p90=min(treatment_terror_m3y_p90)

g s_treatment_terror_m3y_p90=1 if max_treatment_terror_m3y_p90==1 & min_treatment_terror_m3y	 

cap drop date 
gen date = dofm(startmy)
cap drop year
cap drop month
gen year = year(date)
gen month = month(date)



** Ensure there is ppl in control and treated within the same year and state
cap drop date2
g date2=startmy if treatment_terror_m3y_p90==1
format date2 %tm
replace  date2=startmy+3 if treatment_terror_m3y_p90==0
g year2=year(dofm(date2))


foreach x in treatment_terror_m3y_p90 {
		cap drop max_`x' min_`x'
bysort ieb_nat date2 state: egen max_`x'=max(`x')
bysort ieb_nat date2 state: egen min_`x'=min(`x')
cap drop k_`x'_st
g k_`x'_st=1 if max_`x'==1 & min_`x'==0
cap drop max_`x' min_`x'
		}
		

********************************************************************************
**# PLACEBO REGRESSION + SAVE RESULTS
********************************************************************************

	
* Re-employment 

eststo clear
local i=1
foreach out in unemp_1m unemp_3m  {
eststo mod`i': reghdfe `out' treatment_terror_m3y_p90 i.edu_max age frau c.ysm_atunemp##c.ysm_atunemp ln_wage_any_lb ln_firmsize if k_treatment_terror_m3y_p90_st==1 & register_unemp==1 , absorb(i.ieb_nat_id#i.year2  i.month#i.year2 i.state )  vce(cluster cluster_se)

tempfile `out'_`rep'_all
cd "$final/placebo"
parmest, idnum(`rep') idstr("`out'") saving(`"`out'_`rep'_all"', replace)

local i=`i'+1
	}
}

local outcome unemp_1m unemp_3m  
	
foreach out in unemp_1m unemp_3m   {
	
	clear 
	forvalues rep = 1/300 {
		append using "$final/placebo/`out'_`rep'_all.dta"
		}
		
		save "$final/placebo/`out'_300rep_final.dta", replace 	
		}
		
		
	
********************************************************************************
**# Figure G.3
********************************************************************************
foreach out in unemp_1m unemp_3m  {

* 1. LOAD DATASET OF ESTIMATES 

use "$final/placebo/`out'_300rep_final.dta", clear 

global register_1m = "0.009"
global register_3m = "0.017"
global unemp_1m = "0.009"
global unemp_3m = "0.018"

* 2. GENERATE ID OF COEFFICIENT

bys idnum: gen n = _n 
keep if n == 1 			// drop the constant 

* 3. SORT BY SIZE OF COEFFICIENT AND GIVE NUMBER

sort estimate

sum estimate,d 

local rmean_b = round(`r(mean)',0.001)

sum stderr, d
local rmean_se = round(`r(mean)',0.001)

cap drop order
gen order = _n 

* 4. PLOTS

kdensity estimate,  graphregion(color(white))  plotregion(lcolor(black)) ///
ylabel(none, nogrid) ytitle("") title("") xline(${`out'}, lcolor(cranberry)) xtitle("Placebo coefficients", margin(medsmall)) xlabel(,nogrid) ///
note("Average coefficient size (standard error): `rmean_b' (`rmean_se')")

sum estimate stderr

graph save "Graph" "$graph/`out'_placebo_300rep_kdensity.gph", replace
graph export "$graph/`out'_placebo_300rep_kdensity.pdf", as(pdf) replace
	}
